#创建一个浏览器对象
import datetime
import os.path
import sys
from selenium import webdriver
from selenium.webdriver.chrome.service import  Service
from webdriver_manager.chrome import ChromeDriverManager



class Driver:
    driver = ""
    def __init__(self):
        # 浏览器参数
        options = webdriver.ChromeOptions()
        options.page_load_strategy = 'eager'
        # 驱动对象,打开浏览器
        self.driver = webdriver.Chrome(service = Service(ChromeDriverManager().install()),options=options)
        # 隐式等待
        self.driver.implicitly_wait(2)

    def getScreeShont(self):
        #创建屏幕截图
        #图片文件格式： ./2025-04-25-245543.png
        #图片路径：../images/2025-04-25/2025-04-25-245543.png
        dirname = datetime.datetime.now().strftime("%y-%m-%d")
        #判断dirname文件夹是否已经存在
        if not os.path.exists("../images/"+dirname):    #./images/2025-04-25
            #若不存在则创建文件夹
            os.mkdir("../images/"+dirname)
        filename = sys._getframe().f_back.f_code.co_name+'-'+datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")+'.png'
        self.driver.save_screenshot("../images/"+dirname+'/'+filename)
BlogDriver = Driver()